package service;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.PageSql;
import model.MonitoringFacilities;
import model.Staff;
import model.Station;

public class MonitoringFacilitiesService extends BaseService {

	public Page<Record> getPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tmf.id,tmf.area,tmf.controlMode,tmf.newworkOperator,tmf.ip,tmf.platoonIp,tmf.stationIp,tmf.polutionIp,tmf.otherIp,tmf.sampleNo,tmf.manufacture,tmf.regulatoryAuthorities,tmf.industry,ts.name,tc.name AS companyName,tsf.name AS staffName1,tsr.name AS servicerName2 " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_monitoring_facilities tmf ") ;
	    pageSql.join(" t_station ts " , " ts.id = tmf.stationId ") ;
	    pageSql.join(" t_company tc " , " tc.id = ts.companyId ") ;
	    pageSql.join(" t_staff tsf " , " tsf.id = ts.staffId ") ;
	    pageSql.join(" t_servicer tsr " , " tsr.id = tsf.servicerId ") ;
		pageSql.setEqualCondition("tmf.stationId") ; //点位	 
		pageSql.setLikeCondition("ts.name") ; //点位名称	 
		pageSql.setLikeCondition("tc.name") ; //企业名称	 
		pageSql.setLikeCondition("tsr.name") ; //运维公司	 
	    pageSql.orderBy(" tmf.createTime DESC ");
		return pageSql.paginate( pageNumber , pageSize) ;
	}

	public Ret save(MonitoringFacilities monitoringFacilities , Station station , Staff staff) {
	    MonitoringFacilities ex = MonitoringFacilities.dao.findFirstByStationId(station.getId());
	    if(null == ex){
	    	monitoringFacilities.setStationId(station.getId());
	        monitoringFacilities.save();
	    }else{
	    	monitoringFacilities.setId(ex.getId());
	        monitoringFacilities.update() ;
	    }
	    station.setStaffId(staff.getId());
	    station.update();
	    return ok() ;
	}
}
